package net.thxjj.mobileServer.controller.mobile;

import com.alibaba.fastjson.JSONObject;
import com.amazonaws.services.iot.client.AWSIotException;
import lombok.extern.slf4j.Slf4j;
import net.thxjj.mobileServer.controller.base.BaseController;
import net.thxjj.mobileServer.entity.*;
import net.thxjj.mobileServer.service.AwsIotDeviceClientComponent;
import net.thxjj.mobileServer.service.AwsIotServerClientComponent;
import net.thxjj.sitermodel.CertificateBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @ClassName:DeviceController
 * @Description:TODO
 * @Autor:TracyHenry
 * @Date:2022/12/18 0018
 * @Version:1.0
 **/
@Slf4j
@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {


    @Autowired
    private AwsIotServerClientComponent awsIotServerClientComponent;

    @Autowired
    private AwsIotDeviceClientComponent awsIotDeviceClientComponent;

    /**
     * 提供给设备动态证书和deviceId
     * @return
     */
    @RequestMapping(value="/createDeviceId",method= RequestMethod.POST)
    @ResponseBody
    public Object createDeviceId(@RequestBody JSONObject jsonObject)  {

        String serial_number = jsonObject.getString("serial_number");
        String sn;
        if(StringUtils.isEmpty(serial_number)){
            sn = UUID.randomUUID().toString().replace("-","");
        }else{
            sn = serial_number;
        }

        log.info("生成设备sn："+sn);

       CertificateBean certificateBean = awsIotServerClientComponent.createCert();

        if(certificateBean==null){
            return ResponseModel.getModel(ResultEnum.CERTIFICATION_CREATE_FAIL,null);
        }

        log.info("生成证书："+certificateBean.toString());

        boolean flag_thingName = awsIotServerClientComponent.createThing(sn, Const.PRODUCT_199);
        if(!flag_thingName){
            return ResponseModel.getModel(ResultEnum.THING_NAME_CREATE_FAIL,null);
        }
        awsIotServerClientComponent.bindCertAndPolicy(certificateBean.getCertificateArn(), Const.POLICY);

        awsIotServerClientComponent.bindThingAndCert(certificateBean.getCertificateArn(),sn);

        return  ResponseModel.getModel(ResultEnum.SUCCESS,null);
    }


    /**
     * test
     * @return
     */
    @RequestMapping(value="/test",method= RequestMethod.POST)
    @ResponseBody
    public Object test(@RequestBody JSONObject jsonObject)  {


        String aa = "{\"hw_id\":\"000c2933eaa6\",\"msg\":{\"CMD_CODE\":1,\"str1\":\"3769000603\",\"str2\":\"010D01\",\"str3\":\"\"},\"msg_id\":13,\"namespace\":\"config\",\"source\":\"app\",\"target\":\"gateway\"}";
        DataBean dataBean = JSONObject.parseObject(aa,DataBean.class);
        try {
            awsIotDeviceClientComponent.pushMessage("st/siterlink/to_gw/000c2933eaa6_123456",dataBean);
        } catch (AWSIotException e) {
            e.printStackTrace();
        }

        return  ResponseModel.getModel(ResultEnum.SUCCESS,null);
    }


}
